<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="字典名称">
        <el-input v-model="q.name" placeholder="请输入字典名称" clearable />
      </el-form-item>
      <el-form-item label="字典编码">
        <el-input v-model="q.code" placeholder="请输入字典编码" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="addDict">新增字典</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="dictList"
      border
      :header-cell-style="{ background: '#fafafa' }"
    >
      <el-table-column prop="name" label="字典名称" />
      <el-table-column prop="code" label="字典编码" />
      <el-table-column prop="desc" label="描述" />
      <el-table-column prop="disabled" label="是否禁用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch :value="scope.row.disabled?false:true" disabled />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="editDict(scope.row)">编辑</el-button>
          <el-button type="text" @click="setDict(scope.row)">字典配置</el-button>
          <el-button type="text" @click="deleteDict(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="page" :limit.sync="limit" @pagination="fetchData" />

    <el-dialog :title="dictDialogTitle" :visible.sync="dictDialogShow" width="30%">
      <el-form ref="dictForm" :model="dictForm" :rules="dictFormRules" label-width="90px">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="dictForm.name" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input v-model="dictForm.code" placeholder="请输入字典编码" />
        </el-form-item>
        <el-form-item label="是否禁用" prop="disabled">
          <el-radio-group v-model="dictForm.disabled">
            <el-radio :label="false">启用</el-radio>
            <el-radio :label="true">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="dictForm.desc" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dictDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="saveSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-drawer title="字典配置" :visible.sync="dictSetDrawerShow" direction="rtl" size="40%">
      <div class="drawer-container-scroll">
        <el-form :inline="true">
          <el-form-item label="标签名">
            <el-input v-model="qDetail" placeholder="请输入标签名" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="fetchDictDetail">查询</el-button>
            <el-button type="primary" @click="addDictDetail">新增字典标签</el-button>
          </el-form-item>
        </el-form>
        <el-table
          v-loading="listDetailLoading"
          :data="dictDetailList"
          border
          :header-cell-style="{ background: '#fafafa' }"
        >
          <el-table-column label="字典标签" prop="label" />
          <el-table-column label="字典键值" prop="value" />
          <el-table-column label="字典排序" prop="order" />
          <el-table-column label="是否禁用" prop="status">
            <template slot-scope="scope">
              <el-switch :value="scope.row.disabled?false:true" disabled />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="editDictDetail(scope.row)">编辑</el-button>
              <el-button type="text" @click="deleteDictDetail(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="totalDetail > 0" :total="totalDetail" :page.sync="pageDetail" :limit.sync="limitDetail" @pagination="fetchDictDetail" />
      </div>
    </el-drawer>

    <el-dialog :title="dictDetailDialogTitle" :visible.sync="dictDetailDialogShow" width="30%">
      <el-form ref="dictDetailForm" :model="dictDetailForm" :rules="dictDetailFormRules" label-width="90px">
        <el-form-item label="字典标签" prop="label">
          <el-input v-model="dictDetailForm.label" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典键值" prop="value">
          <el-input v-model="dictDetailForm.value" placeholder="请输入字典编码" />
        </el-form-item>
        <el-form-item label="是否禁用" prop="disabled">
          <el-radio-group v-model="dictDetailForm.disabled">
            <el-radio :label="false">启用</el-radio>
            <el-radio :label="true">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="order">
          <el-input-number v-model="dictDetailForm.order" :min="1" :max="999" label="排序序号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dictDetailDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="saveDictDetailSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import apiDict from '@/api/system/dict'
import Pagination from '@/components/Pagination'
const defaultForm = {
  id: '',
  name: '',
  code: '',
  disabled: false,
  desc: ''
}
const defaultDictDetailForm = {
  id: '',
  dict_id: '',
  label: '',
  value: '',
  disabled: false,
  order: ''
}
export default {
  components: { Pagination },
  data() {
    return {
      dictList: [],
      dictDetailList: [],
      listLoading: false,
      listDetailLoading: false,
      dictDialogShow: false,
      dictDialogTitle: '新增字典',
      dictSetDrawerShow: false,
      dictDetailDialogTitle: '新增字典标签',
      dictDetailDialogShow: false,
      dictForm: defaultForm,
      dictDetailForm: defaultDictDetailForm,
      setDictId: '',
      q: {
        name: '',
        code: ''
      },
      page: 1,
      limit: 10,
      total: 0,
      qDetail: '',
      pageDetail: 1,
      limitDetail: 10,
      totalDetail: 0,
      dictFormRules: {
        name: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '字典编码不能为空', trigger: 'blur' }
        ],
        disabled: [
          { required: true, message: '是否禁用不能为空', trigger: 'blur' }
        ]
      },
      dictDetailFormRules: {
        label: [
          { required: true, message: '字典标签不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '字典键值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = {
        page: this.page,
        limit: this.limit,
        code: this.q.code,
        name: this.q.name
      }
      apiDict.getDictList(params).then(response => {
        this.dictList = response.data
        this.total = response.count
      })
      this.listLoading = false
    },
    addDict() {
      this.dictDialogShow = true
      this.dictDialogTitle = '新增字典'
      this.submitType = 0
      this.dictForm = Object.assign({}, defaultForm)
    },
    editDict(row) {
      this.dictDialogShow = true
      this.dictDialogTitle = '编辑字典'
      this.submitType = 1
      this.dictForm = Object.assign({}, row)
    },
    async saveSubmit() {
      await this.$refs.dictForm.validate(async validate => {
        if (validate) {
          if (this.submitType === 0) {
            await apiDict.createDict(this.dictForm).then(response => {
              this.$message.success(response.message || '新增成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          } else {
            await apiDict.updateDict(this.dictForm).then(response => {
              this.$message.success(response.message || '编辑成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          }
          this.dictDialogShow = false
          await this.fetchData()
        }
      })
    },
    deleteDict(row) {
      const data = [row.id]
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }).then(async() => {
        await apiDict.deleteDict(data).then(async response => {
          this.$message.success(response.message || '删除成功')
          await this.fetchData()
        })
      })
    },
    handleCloseDrawer() {
      this.dictSetDrawerShow = false
    },
    async setDict(row) {
      this.dictSetDrawerShow = true
      defaultDictDetailForm.dict_id = row.id
      this.setDictId = row.id
      await this.fetchDictDetail()
    },
    fetchDictDetail() {
      this.listDetailLoading = true
      const params = {
        page: this.pageDetail,
        limit: this.limitDetail,
        label: this.qDetail,
        dict_id: this.setDictId
      }
      apiDict.getDictDetailList(params).then(response => {
        this.dictDetailList = response.data
        this.totalDetail = response.count
      })
      this.listDetailLoading = false
    },
    addDictDetail() {
      this.dictDetailDialogShow = true
      this.dictDetailDialogTitle = '新增字典标签'
      this.submitType = 0
      this.dictDetailForm = Object.assign({}, defaultDictDetailForm)
    },
    editDictDetail(row) {
      this.dictDetailDialogShow = true
      this.dictDialogTitle = '编辑字典标签'
      this.submitType = 1
      this.dictDetailForm = Object.assign({}, row)
    },
    async saveDictDetailSubmit() {
      await this.$refs.dictDetailForm.validate(async validate => {
        if (validate) {
          if (this.submitType === 0) {
            await apiDict.createDictDetail(this.dictDetailForm).then(response => {
              this.$message.success(response.message || '新增成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          } else {
            await apiDict.updateDictDetail(this.dictDetailForm).then(response => {
              this.$message.success(response.message || '编辑成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          }
          this.dictDetailDialogShow = false
          this.saveDetailLoading = false
          await this.fetchDictDetail()
        }
      })
    },
    deleteDictDetail(row) {
      const data = [row.id]
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }).then(async() => {
        await apiDict.deleteDictDetail(data).then(async response => {
          this.$message.success(response.message || '删除成功')
          await this.fetchDictDetail()
        })
      })
    }
  }
}
</script>
